package cn.edu.hitwh.kanlema.interceptor;

import cn.edu.hitwh.kanlema.dto.UserDTO;
import cn.edu.hitwh.kanlema.util.SignUtil;
import cn.edu.hitwh.kanlema.util.UserHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredArgsConstructor
public class SignInterceptor implements HandlerInterceptor {
    private final SignUtil signUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserDTO userDTO = UserHolder.tryGetUser();
        if (userDTO == null) {
            return true;
        }

        Long userId = userDTO.getId();
        signUtil.sign(userId);

        return true;
    }
}
